﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;
using InfoJetSoft.SharePointPeople;

//CID 0800 //2011.03.15 //Begin
namespace InfoJetSoft.Service.Worker
{
    class DropdownListItemsLoadWorker
    {
        public static string loadItems(Form form)
        {
            string ajaxBracket = FormUtil.getAjaxBracket(form);
            
            StringBuilder buffer = new StringBuilder();
            buffer.Append(ajaxBracket);
            string parent = "";
            if (ConfigUtil.UseAJAX())
            {
                parent = "";
            }
            else
            {
                buffer.Append("<script>");
                parent = "parent.";
            }
            buffer.Append(parent + "InfoJet_StopProgress();");
            buffer.Append(parent + "InfoJet_Postbacking = 0;");
            buffer.Append(parent + "InfoJet_OnPostbackEnd();");

            try
            {
                string formId = form.HttpContext.Request.Params["xdoc_param_form_id"];
                string dropdownListId = form.HttpContext.Request.Params["xdoc_param_dropdownlist_id"];
                string contextId = form.HttpContext.Request.Params["xdoc_param_dropdownlist_context_id"];
                string buttonId = form.HttpContext.Request.Params["xdoc_param_dropdownlist_button_id"];

                int index = int.Parse(dropdownListId);
                DropdownListInfo info = (DropdownListInfo)form.Manifest.DrondownListInfos[index];

                //CID 0800 //2012.03.01 //Begin
                XPathNodeIterator itemsIterator = XPathEvaluator.evaluateNodeList(form, form.XmlDoc, info.ItemsPath);
                //CID 0800 //2012.03.01 //End

                StringBuilder htmlBuffer = new StringBuilder();
                htmlBuffer.Append("<select size='10' onclick='InfoJet_OnPopupDropdownListClick(event, this);' onblur='InfoJet_OnPopupDropdownListBlur(this);' onkeypress='InfoJet_OnPopupDropdownListKeyPress(event, this);'>");
                //CID 0800 //2012.03.01 //Begin
                while (itemsIterator.MoveNext())
                {
                    XPathNavigator itemNav = itemsIterator.Current;
                    XPathNavigator nameNav = itemNav.SelectSingleNode(info.NamePath, form.Manifest.NsResolver);
                    XPathNavigator valueNav = itemNav.SelectSingleNode(info.ValuePath, form.Manifest.NsResolver);
                    
                    string name = nameNav.Value;
                    name = name.Replace("\"", "\\\"");
                    name = name.Replace("\r", "\\r");
                    name = name.Replace("\n", "\\n");

                    string value = valueNav.Value;
                    value = value.Replace("\"", "\\\"");
                    value = value.Replace("\r", "\\r");
                    value = value.Replace("\n", "\\n");

                    htmlBuffer.Append("<option value=\\\"" + value + "\\\">");
                    htmlBuffer.Append(name);
                    htmlBuffer.Append("</option>");
                }
                //CID 0800 //2012.03.01 //End
                htmlBuffer.Append("</select>");
                string selectHtml = htmlBuffer.ToString();

                buffer.Append(parent + "InfoJet_PopupDropdownList(\"" + buttonId + "\", \"" + selectHtml + "\");");
            }
            catch (Exception e)
            {
                buffer.Append(parent + "alert(\"" + e.Message + "\");");
            }

            if (!ConfigUtil.UseAJAX())
            {
                buffer.Append("</script>");
            }

            buffer.Append(ajaxBracket);
            return buffer.ToString();
        }
    }
}
//CID 0800 //2011.03.20 //End